#include <bits/stdc++.h>
#define int long long
const int inf=0x3f3f3f3f;
using namespace std;
const int maxn=2e5+5;
int t,a[maxn],k,n,pre[maxn];
bool Judge(int len,int x) {
    int sum=pre[n-len]-a[1]+x*(len+1);
    return sum<=k;
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>t;
    while(t--) {
        cin >>n>>k;
        for(int i=1; i<=n; i++)cin >>a[i];//记录数据
        sort(a+1,a+1+n);//排序
        for(int i=1; i<=n; i++)pre[i]=pre[i-1]+a[i];
        //前缀和
        int res=2*inf,ans=0;
        for(int i=0; i<=n-1; i++) {//i为最大的i个数被赋值为a[1]
            int l=-inf,r=inf;
            while(l<r) {
                int mid=l+r+1>>1;
                if(Judge(i,mid))//mid为对a[1]进行几次-1
                    l=mid;
                else
                    r=mid-1;
            }
            res=min(res,max(0ll,a[1]-l)+i);
            //a[1]-l为对a[1]减了几次，为负值代表不用减就能满足
        }
        cout <<res<<endl;
    }
    return 0;
}
